package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.LinkedList;
import java.util.List;

/**
 * https://leetcode.cn/problems/validate-binary-search-tree/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-06-14 20:28
 */
public class Test43 {
    public static void main(String[] args) {

    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isValidBST(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        midSearch(root, list);
        int count = 0;
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i).equals(list.get(i - 1))) {
                if (count == 0) {
                    count++;
                }
                if (count == 1) {
                    return false;
                }
            }
            if (list.get(i) < list.get(i - 1)) {
                return false;
            }
        }
        return true;
    }

    public void midSearch(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        midSearch(root.left, list);
        list.add(root.val);
        midSearch(root.right, list);
    }


}
